package com.kaoshi.myweb.common.utils.net;

import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import org.apache.http.client.HttpClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;

public class HttpUtil {

	private static final Logger logger = LoggerFactory.getLogger(HttpUtil.class);

	public static void main(String arg[]) throws Exception {
		String json = "{ \"isNewFlow\":\"1\", \"requestSysId\":1423210758571, \"requestSysName\":\"FMS\", \"taskComp\":\"PA011_S000000205\", \"taskDept\":\"PA011_S000033562\", \"taskDeptName\":\"集团行政组\", \"taskDesc\":\"我来测试万里通\", \"taskDetailList\":[{\"flowOwner\":\"PAICDOM\\ZHONGXIANGBO952\", \"handType\":\"30\", \"nodeDesc\":\"第一步审批\", \"seq\":\"1\"}, {\"flowOwner\":\"PAICDOM\\CAIJING1\", \"handType\":\"30\", \"nodeDesc\":\"第二步审批\", \"seq\":\"2\"}], \"taskFileList\":[{\"contentType\":\"html\", \"docClass\":\"FMSClass_url\", \"docName\":\"http://cnsz020118:5000/Page/Standard/StandardInfoForEoa.aspx?ID=a6c1657a-11a2-4355-8f8c-db36dd88d9e6\", \"docOldName\":\"测试附件链接地址\", \"fileSize\":\"50\", \"operator\":\"中向波\", \"requestSysId\":1423210758571, \"requestSysName\":\"FMS\"}], \"taskName\":\"万里通测试\", \"taskOwner\":\"PAICDOM\\ZHANGWENTAO446\", \"taskSendLeader\":\"审批领导\", \"taskSortId\":\"1\", \"taskTransactor\":\"ZHANGWENTAO446 650249\" }";

		String url = "http://10.20.12.90:7999/open/appsvr/group/task/import";

		// System.out.println(doPost(url, json));
	}

	/**
	 * get请求
	 * 
	 * @return
	 * @throws Exception
	 */
	public static String doRestGet(String url) throws Exception {
		logger.info("url：" + url);
		RestTemplate restTemplate = RestClient.getClient();

		HttpHeaders headers = new HttpHeaders();
		headers.setAccept(Arrays.asList(new MediaType[] { new MediaType("application", "json", Charset.forName("UTF-8")) }));
		headers.setContentType(new MediaType("application", "json", Charset.forName("UTF-8")));

		HttpEntity<Map> requestEntity = new HttpEntity<Map>(headers);

		ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.GET, requestEntity, String.class);
		String result = response.getBody();
		logger.info("http返回结果：" + result);
		return result;
	}

	public static String doRestPost(String url, Map<String, Object> paraMap) {

		logger.info("url：" + url);
		logger.info("输入参数：" + paraMap);
		RestTemplate restTemplate = RestClient.getClient();

		HttpHeaders headers = new HttpHeaders();
		headers.setAccept(Arrays.asList(new MediaType[] { new MediaType("application", "json", Charset.forName("UTF-8")) }));
		headers.setContentType(new MediaType("application", "json", Charset.forName("UTF-8")));
		HttpEntity<Map> requestEntity = new HttpEntity<Map>(paraMap, headers);
		ResponseEntity<String> response = restTemplate.postForEntity(url, requestEntity, String.class);
		String result = response.getBody();

		logger.info("http返回结果：" + result);
		return result;
	}
}
